package com.ddz.implementation;

import java.io.IOException;

import android.app.Activity;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.media.AudioManager;
import android.media.SoundPool;

public class Audio {
	AssetManager mAssets;
	SoundPool mSounds;

	public Audio(Activity activity) {
		activity.setVolumeControlStream(AudioManager.STREAM_MUSIC);
		mAssets = activity.getAssets();
		mSounds = new SoundPool(20, AudioManager.STREAM_MUSIC, 0);
	}


	// Send in file path/name to create usable music
	// source from asset
	public Music createMusic(String fileName) {
		try {
			AssetFileDescriptor assetDescriptor = mAssets.openFd(fileName);
			return new Music(assetDescriptor);
		} catch (IOException e) {
			throw new RuntimeException("Couldn't load music '" + fileName + "'");
		}
	}

	// Send in file path/name to create usable sound
	// source from asset
	public Sound createSound(String fileName) {
		try {
			AssetFileDescriptor assetDescriptor = mAssets.openFd(fileName);
			int soundId = mSounds.load(assetDescriptor, 0);
			return new Sound(mSounds, soundId);
		} catch (IOException e) {
			throw new RuntimeException("Couldn't load sound '" + fileName + "'");
		}
	}
}
